package com.mobile.service.impl;

import com.github.pagehelper.PageInfo;
import com.mobile.service.convert.PensionDeclarationConverter;
import com.mobile.service.dao.DaoSupport;
import com.mobile.service.dto.PensionDeclarationDTO;
import com.mobile.service.dto.PensionDeclarationQueryDTO;
import com.mobile.service.entity.PensionDeclaration;
import com.mobile.service.inter.PensionDeclarationService;
import com.mobile.service.mapper.PensionDeclarationMapper;
import com.mobile.service.util.PageUtils;
import com.mobile.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 养老保险申请表service实现类
 */
@Service("pensionDeclarationService")
@Transactional
public class PensionDeclarationServiceImpl implements PensionDeclarationService {
    @Resource(name = "daoSupport")
    private DaoSupport dao;
    @Autowired
    private PensionDeclarationConverter pensionDeclarationConverter;
    @Autowired
    private PensionDeclarationMapper pensionDeclarationMapper;

    @Override
    public Map<String, Object> addPensionDeclaration(PensionDeclarationDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        PensionDeclarationDTO pd = pensionDeclarationMapper.getPensionDeclarationByApplicantName(dto.getApplicantName());
        if (null == pd) {
            pensionDeclarationMapper.insertPensionDeclaration(pensionDeclarationConverter.toEntity(dto));
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0011"));
        } else {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该申请人姓名"));
        }
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public PageResult<PensionDeclarationDTO> getPensionDeclarationListPage(PensionDeclarationQueryDTO queryDTO) throws Exception {
        PageUtils.startPage(queryDTO);
        List<PensionDeclarationDTO> list = pensionDeclarationMapper.pensionDeclarationlistPage(queryDTO);
        return new PageResult<PensionDeclarationDTO>(list);
    }

    @Override
    public PensionDeclarationDTO getPensionDeclarationById(String id) throws Exception {
        return pensionDeclarationMapper.getPensionDeclarationById(id);
    }

    @Override
    public Map<String, Object> updatePensionDeclaration(PensionDeclarationDTO dto) throws Exception {
        Map<String, Object> map = new HashMap<>();
        String pkValue = dto.getId();
        if (pkValue == null) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        PensionDeclarationDTO data = this.getPensionDeclarationById(pkValue);
        if (Objects.isNull(data)) {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0001"));
            return map;
        }

        PensionDeclarationDTO pd = pensionDeclarationMapper.getPensionDeclarationByApplicantName(dto.getApplicantName());
        if (null == pd || pd.getId().equals(pkValue)) {
            pensionDeclarationMapper.updatePensionDeclaration(pensionDeclarationConverter.toEntity(dto));
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0009"));
        } else {
            map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_FAILED);
            map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("E0004", "该申请人姓名"));
        }
        return map;
    }

    @Override
    public Map<String, Object> removePensionDeclaration(String id) throws Exception {
        Map<String, Object> map = new HashMap<>();
        pensionDeclarationMapper.deletePensionDeclaration(id);
        map.put(Const.ACTION_RESULT, Const.ACTION_RESULT_SUCCESS);
        map.put(Const.ACTION_MESSAGE, MessageUtil.getMessage("I0010"));
        return map;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<PensionDeclarationDTO> getPensionDeclarationList(PensionDeclarationQueryDTO queryDTO) throws Exception {
        return pensionDeclarationMapper.getPensionDeclarationList(queryDTO);
    }
}
